﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using AutoMapper.Mappers;
using Ninject;
using Ninject.Modules;
using DataContracts = Jarvis.WCF.DataContracts;

namespace Jarvis.Server.IoC
{
    public class AutoMapperModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ITypeMapFactory>().To<TypeMapFactory>();
            foreach (var mapper in MapperRegistry.AllMappers())
                Bind<IObjectMapper>().ToConstant(mapper);
            Bind<ConfigurationStore>().ToSelf().InSingletonScope()
                .WithConstructorArgument("mappers",
                    ctx => ctx.Kernel.GetAll<IObjectMapper>());
            Bind<IConfiguration>().ToMethod(ctx => ctx.Kernel.Get<ConfigurationStore>());
            Bind<IConfigurationProvider>().ToMethod(ctx =>
                ctx.Kernel.Get<ConfigurationStore>());
            Bind<IMappingEngine>().To<MappingEngine>();

            MapDomainToDto(Kernel.Get<IConfiguration>());
        }

        private void MapDomainToDto(IConfiguration configuration)
        {
            configuration.CreateMap<Domain.Location, DataContracts.Location>();
            configuration.CreateMap<Domain.LocationCategory, DataContracts.LocationCategory>();
            configuration.CreateMap<Domain.Action, DataContracts.Action>()
                .Include<Domain.ExecuteProgramAction, DataContracts.ExecuteProgramAction>()
                .Include<Domain.ShowMessageAction, DataContracts.ShowMessageAction>()
                .Include<Domain.TerminateProgramAction, DataContracts.TerminateProgramAction>();
            configuration.CreateMap<Domain.ExecuteProgramAction, DataContracts.ExecuteProgramAction>();
            configuration.CreateMap<Domain.ShowMessageAction, DataContracts.ShowMessageAction>();
            configuration.CreateMap<Domain.TerminateProgramAction, DataContracts.TerminateProgramAction>();
            configuration.CreateMap<Domain.LocationStats, DataContracts.LocationStats>();
            configuration.CreateMap<Domain.LocationStatsEntry, DataContracts.LocationStatsEntry>();

            configuration.CreateMap<DataContracts.Location, Domain.Location>();
            configuration.CreateMap<DataContracts.LocationCategory, Domain.LocationCategory>();
            configuration.CreateMap<DataContracts.Action, Domain.Action>()
                .Include<DataContracts.ExecuteProgramAction, Domain.ExecuteProgramAction>()
                .Include<DataContracts.ShowMessageAction, Domain.ShowMessageAction>()
                .Include<DataContracts.TerminateProgramAction, Domain.TerminateProgramAction>();
            configuration.CreateMap<DataContracts.ExecuteProgramAction, Domain.ExecuteProgramAction>();
            configuration.CreateMap<DataContracts.ShowMessageAction, Domain.ShowMessageAction>();
            configuration.CreateMap<DataContracts.TerminateProgramAction, Domain.TerminateProgramAction>();

        }
    }
}
